package com.example.administrator.demoa;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {
    private static final int MSG_PROGRESS_CHANGED = 1001;
    private Button btnStart;
    private Button btnPause;
    private Button btnStop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private TextView txtCur;
    private TextView txtTotal;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what == MSG_PROGRESS_CHANGED){
                    int position = msg.arg1;
                    int total = msg.arg2;
                    /*//1.
                    SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
                    txtCur.setText(sdf.format(position));
                    txtTotal.setText(sdf.format(total));*/

                    //2.
                    txtCur.setText(changeFormat(position));
                    txtTotal.setText(changeFormat(total));

                    return true;
                }
                return false;
            }
        });

        //进度条随音乐的播放而改变
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if(mediaPlayer != null && mediaPlayer.isPlaying()) {
                        //获取当前位置
                        int position = mediaPlayer.getCurrentPosition();
                        //获取总时长
                        int total = mediaPlayer.getDuration();
                        //进度
                        int progress = 100 * position / total;
                        //seekBar进度的设置
                        seekBar.setProgress(progress);

                        //子线程中不能直接修改UI界面，可通过消息机制
                        Message message = new Message();
                        message.what = MSG_PROGRESS_CHANGED;
                        message.arg1 = position;
                        message.arg2 = total;
                        handler.sendMessage(message);
                    }

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        BtnStart();
        BtnPause();
        BtnStop();
        initSeekBar();
    }

    private String changeFormat(int position) {
        int second = position/1000;
        int min = second/60;
        second = second%60;

        StringBuffer buffer = new StringBuffer();
        if(min <10){
            buffer.append("0");
        }

        buffer.append(min).append(":");

        if(second < 10){
            buffer.append("0");
        }

        buffer.append(second);

        return buffer.toString();
    }

    private void initSeekBar() {
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //音频播放随进度条的改变而改变
                if(mediaPlayer != null) {
                    //获取进度条当前的进度
                    int progress = seekBar.getProgress();
                    int total = mediaPlayer.getDuration();
                    int position = total * progress / 100;
                    mediaPlayer.seekTo(position);
                }
            }
        });
    }

    private void BtnStart() {
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();

                    try {
                        mediaPlayer.setDataSource("/mnt/shared/Other/shangfeng.aac");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    mediaPlayer.prepareAsync();

                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();
                        }
                    });
                }else {
                    mediaPlayer.start();
                }
            }
        });
    }

    private void BtnPause() {
        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.pause();
            }
        });
    }

    private void BtnStop(){
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
            }
        });
    }

    private void findViews() {
        btnStart = findViewById(R.id.btn_start);
        btnStop = findViewById(R.id.btn_stop);
        btnPause = findViewById(R.id.btn_pause);
        seekBar = findViewById(R.id.seek_bar);
        txtCur = findViewById(R.id.txt_current);
        txtTotal = findViewById(R.id.txt_total);
    }
}
